<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Javascript的构建对象</h1>
<hr>

<script>
    //1.字面量创建对象
    /*
    let person = {};
    person.name = '张三';
    person.age = 18;
    person.addr = '西安市';
    console.log(person);
    console.log(person instanceof Object);
    */

    //上面的写法还不够标准，推荐下面的定义方式
    /*
    let person = {
        name: '张三',
        age :18,
        addr: '西安市',
        sayHello:function(){
            console.log("hello,my name is "+this.name);
        }
    };
    person.sayHello();
    console.log(person);
    console.log(person instanceof Object);
    */


    //2.构造函数法
    /*
    function Person(name,age,addr){
        this.name = name;
        this.age = age;
        this.addr = addr;
        this.sayHello=function(){
            console.log("hello,my name is "+this.name);
        }
    }
    let person = new Person('张三',18,'西安市');
    person.sayHello();
    console.log(person);
    console.log(person instanceof Object);
    console.log(person instanceof Person);
    */

    //3.使用工厂方式
    /*
    let person = new Object();
    person.name = '张三';
    person.age = 18;
    person.addr = '西安市';
    person.sayHello=function(){console.log("hello,my name is "+this.name);};
    person.sayHello();
    console.log(person);
    console.log(person instanceof Object);
    */

    //4.混合模式
    function Person(name,age,addr){
        this.name = name;
        this.age = age;
        this.addr = addr;
    }
    Person.prototype.sayHello=function(){console.log("hello,my name is "+this.name);}
    let person = new Person('张三',18,'西安市');
    person.sayHello();
    console.log(person);
    console.log(person instanceof Object);


</script>
</body>
</html>